package erp

import (
	"context"
	"encoding/json"
	"ez/apps/erp/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type BuyOrder struct {
	mgo.BaseDoc   `bson:"-" json:"-"`
	Id            int64              `json:"id" bson:"id,omitempty"`
	Sn            string             `json:"sn" bson:"sn"`                       //单据编号
	Title         string             `json:"title" bson:"title"`                 //单据主题
	Seller        *Seller            `json:"seller" bson:"seller"`               //供应商
	SellerId      int64              `json:"sellerId" bson:"sellerId"`           //供应商
	SellerName    string             `json:"sellerName" bson:"sellerName"`       //供应商名称
	TaxType       *TaxType           `json:"taxType" bson:"taxType"`             //发票类型
	TaxTypeId     int64              `json:"taxTypeId" bson:"taxTypeId"`         //发票类型
	PayType       *PayType           `json:"payType" bson:"payType"`             //付款方式
	PayTypeId     int64              `json:"payTypeId" bson:"payTypeId"`         //付款方式
	PayCashType   *PayCashType       `json:"payCashType" bson:"payCashType"`     //结算方式
	PayCashTypeId int64              `json:"payCashTypeId" bson:"payCashTypeId"` //结算方式
	DeliverType   *DeliverType       `json:"deliverType" bson:"deliverType"`     //交货方式
	DeliverTypeId int64              `json:"deliverTypeId" bson:"deliverTypeId"` //交货方式
	DeliverAt     time.Time          `json:"deliverAt" bson:"deliverAt"`         //交货时间
	Deliver       string             `json:"deliver" bson:"deliver"`             //交货地点
	ContactEmp    *Emp               `json:"contactEmp" bson:"contactEmp"`       //业务员
	ContactEmpId  int64              `json:"contactEmpId" bson:"contactEmpId"`   //业务员
	Contact       string             `json:"contact" bson:"contact"`             //联系人
	ContactPhone  string             `json:"contactPhone" bson:"contactPhone"`   //联系电话
	Num           float64            `json:"num" bson:"num"`                     //数量
	Money         float64            `json:"money" bson:"money"`                 //金额
	Status        *BuyOrderStatus    `json:"status" bson:"status"`               //状态
	StatusId      int64              `json:"statusId" bson:"statusId"`           //状态
	IsConfirm     bool               `json:"isConfirm" bson:"isConfirm"`         //是否生效
	ConfirmAt     time.Time          `json:"confirmAt" bson:"confirmAt"`         //生效时间
	IsCancel      bool               `json:"isCancel" bson:"isCancel"`           //是否取消
	IsLock        bool               `json:"isLock" bson:"isLock"`               //是否锁定
	ApprovalSate  string             `json:"approvalSate" bson:"approvalSate"`   //评审状态
	ApprovalStage string             `json:"approvalStage" bson:"approvalStage"` //评审环节
	FormAt        time.Time          `json:"formAt" bson:"formAt"`               //制单时间
	FormEmp       *Emp               `json:"formEmp" bson:"formEmp"`             //制单人
	FormEmpId     int64              `json:"formEmpId" bson:"formEmpId"`         //制单人
	CreateEmp     *Emp               `json:"createEmp" bson:"createEmp"`         //创建人
	CreateEmpId   int64              `json:"createEmpId" bson:"createEmpId"`     //创建人
	Note          string             `json:"note" bson:"note"`                   //备注
	Product       []*BuyOrderProduct `json:"product" bson:"product"`             //产品
	CreateAt      time.Time          `json:"createAt" bson:"createAt"`
	UpdateAt      time.Time          `json:"updateAt" bson:"updateAt"`
	DeleteAt      *time.Time         `json:"deleteAt" bson:"deleteAt"`
}

func (this *BuyOrder) DocName() string { return "BuyOrder" }
func (this *BuyOrder) GetId() int64    { return this.Id }
func (this *BuyOrder) SetId(id int64)  { this.Id = id }
func (this *BuyOrder) Create() error {
	return this.GetFactory().Create(this)
}
func (this *BuyOrder) Replace() error {
	return this.GetFactory().Replace(this)
}
func (this *BuyOrder) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *BuyOrder) Delete() error {
	return this.GetFactory().Delete(this)
}
func (this *BuyOrder) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *BuyOrder) Destroy() error {
	return this.GetFactory().Destroy(this)
}
func (this *BuyOrder) ToString() string {
	return string(this.ToBytes())
}
func (this *BuyOrder) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *BuyOrder) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *BuyOrder) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
func (this *BuyOrder) LoadSeller() {
	if this.SellerId == 0 {
		return
	}
	this.Seller, _ = NewSellerCrud().FindId(this.SellerId)
}
func (this *BuyOrder) LoadTaxType() {
	if this.TaxTypeId == 0 {
		return
	}
	this.TaxType, _ = NewTaxTypeCrud().FindId(this.TaxTypeId)
}
func (this *BuyOrder) LoadPayType() {
	if this.PayTypeId == 0 {
		return
	}
	this.PayType, _ = NewPayTypeCrud().FindId(this.PayTypeId)
}
func (this *BuyOrder) LoadPayCashType() {
	if this.PayCashTypeId == 0 {
		return
	}
	this.PayCashType, _ = NewPayCashTypeCrud().FindId(this.PayCashTypeId)
}
func (this *BuyOrder) LoadDeliverType() {
	if this.DeliverTypeId == 0 {
		return
	}
	this.DeliverType, _ = NewDeliverTypeCrud().FindId(this.DeliverTypeId)
}
func (this *BuyOrder) LoadContactEmp() {
	if this.ContactEmpId == 0 {
		return
	}
	this.ContactEmp, _ = NewEmpCrud().FindId(this.ContactEmpId)
}
func (this *BuyOrder) LoadStatus() {
	if this.StatusId == 0 {
		return
	}
	this.Status, _ = NewBuyOrderStatusCrud().FindId(this.StatusId)
}
func (this *BuyOrder) LoadFormEmp() {
	if this.FormEmpId == 0 {
		return
	}
	this.FormEmp, _ = NewEmpCrud().FindId(this.FormEmpId)
}
func (this *BuyOrder) LoadCreateEmp() {
	if this.CreateEmpId == 0 {
		return
	}
	this.CreateEmp, _ = NewEmpCrud().FindId(this.CreateEmpId)
}
func (this *BuyOrder) LoadProduct() {
	product, _ := NewBuyOrderProductCrud().FindBy(ss.M{"belongToId": this.Id}, []string{"id"}, 0, 0)
	this.Product = product
}
func (this *BuyOrder) ClearRelationsBeforeSave() mgo.Doc {
	this.Seller = nil
	this.TaxType = nil
	this.PayType = nil
	this.PayCashType = nil
	this.DeliverType = nil
	this.ContactEmp = nil
	this.Status = nil
	this.FormEmp = nil
	this.CreateEmp = nil
	this.Product = nil
	return this
}
func neverUsed_BuyOrder() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type BuyOrderAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *BuyOrder
	Session *ez.Session
}

func NewBuyOrderAccessControl(model *BuyOrder, action string, session *ez.Session) *BuyOrderAccessControl {
	ctrl := &BuyOrderAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.BuyOrderAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
